package com.mdlib.helper.module.send.profile.fragments;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.BalanceEntity;
import com.mdlib.helper.ui.fragments.sect.money.MoneyRelateFragment;
import com.mdlib.helper.ui.fragments.sect.money.RechargeFragment;
import com.mdlib.helper.ui.fragments.sect.money.WithdrawalApplyFragment;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

import static com.mdlib.helper.ui.UIHelper.BUDGET;
import static com.mdlib.helper.ui.UIHelper.WITHDRAWAL;

/**
 * 作者：HQY on 16/11/15 17:18
 * 邮箱：hqy_xz@126.com
 */

public class SendBalacneFragment extends BaseTitleFragment {

    @Bind(R.id.tv_balance_money)
    TextView mTvBalanceMoney;
    @Bind(R.id.tv_cumulative_money)
    TextView mTvCumulativeMoney;
    @Bind(R.id.tv_cumulative_reflect)
    TextView mTvCumulativeReflect;
    @Bind(R.id.iv_balance_details)
    ImageView mIvBalanceDetails;
    @Bind(R.id.rl_balance_bill)
    RelativeLayout mRlBalanceBill;
    @Bind(R.id.iv_balance_withdrawal)
    ImageView mIvBalanceWithdrawal;
    @Bind(R.id.rl_balance_withdrawal)
    RelativeLayout mRlBalanceWithdrawal;
    @Bind(R.id.tv_online_pay)
    TextView mTvOnlinePay;
    @Bind(R.id.tv_apply)
    TextView mTvApply;
    private String money = "0";

    public static SendBalacneFragment newInstance() {
        return new SendBalacneFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_send_balance;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.me_tv_balance, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });


    }

    @Override
    public void onStart() {
        super.onStart();
        getBalance();
    }

    public void getBalance() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi().getBalance()
                .compose(RxHelper.<BaseResult<BalanceEntity>>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult<BalanceEntity>>() {
                    @Override
                    protected void onSucc(BaseResult<BalanceEntity> balanceEntity) {
                        stopLoadingDialog();
                        updateBalance(balanceEntity.data);
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                    }
                }));
    }

    public void updateBalance(BalanceEntity info) {
        money = info.getBalance();
        mTvBalanceMoney.setText("￥" + info.getBalance());
        mTvCumulativeMoney.setText("￥" + info.getSpendSum());
        mTvCumulativeReflect.setText("￥" + info.getWithdrawSum());

    }

    @OnClick({R.id.rl_balance_bill, R.id.rl_balance_withdrawal, R.id.tv_online_pay, R.id.tv_apply})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_balance_bill:
                addFragment(MoneyRelateFragment.newInStance(BUDGET));
                break;
            case R.id.rl_balance_withdrawal:
                addFragment(MoneyRelateFragment.newInStance(WITHDRAWAL));
                break;
            case R.id.tv_online_pay:
                addFragment(RechargeFragment.newInstance());
                break;
            case R.id.tv_apply:
                addFragment(WithdrawalApplyFragment.newInstance(money));
                break;
        }
    }
}
